package com.zjf.controller;

import com.zjf.common.Result;
import com.zjf.entity.Address;
import com.zjf.service.IDeliveryAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/address")
@CrossOrigin
public class AddressController {
    @Resource
    private IDeliveryAddressService addressService;


    @PostMapping("/add")
    public Result<Address> addAddress(@RequestBody Address address){
        addressService.saveDeliveryAddress(address);
        return Result.success(address,"新增地址成功");
    }
    @PutMapping("/update")
    public Result<Address> updateAddress(@RequestBody Address address){
        addressService.updateDeliveryAddress(address);
        return Result.success(address,"修改地址成功");

    }

    @GetMapping("/listByUserId")
    public Result<List<Address>> listAddressByUserId(@RequestParam("userId") String userId){
        List<Address> listAddresses = addressService.listDeliveryAddressByUserId(userId);
        return Result.success(listAddresses,"查询地址成功");
    }
    @GetMapping("/getById")
    public Result<Address> getAddressById(@RequestParam("id") Integer id){
        Address address = addressService.getDeliveryAddressById(id);
        return Result.success(address,"查询地址成功");
    }
    @DeleteMapping("/delete")
    public Result<Address> deleteDeliveryAddress(@RequestParam("id") Integer id){
        addressService.removeDeliveryAddress(id);
        return Result.success("删除地址成功") ;
    }

}
